import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import postcssPresetEnv from 'postcss-preset-env'
import path from 'path'
import { fileURLToPath } from 'url'
import viteCompression from 'vite-plugin-compression'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

// https://vite.dev/config/
export default defineConfig({
  // base: 'react-admin',
  base: '',
  plugins: [
    react(),
    viteCompression({
      verbose: true, // 是否在控制台输出压缩结果
      disable: false, // 是否禁用压缩
      threshold: 10240, // 大于10kb的文件才压缩
      algorithm: 'gzip', // 压缩算法，可选 'gzip' 或 'brotli'
      ext: '.gz' // 压缩后的文件后缀名
    })
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src')
    },
    extensions: ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json']
  },
  css: {
    postcss: {
      plugins: [postcssPresetEnv()]
    }
  },
  server: {
    host: '0.0.0.0',
    port: 4000,
    proxy: {
      '/dev': {
        target: 'http://127.0.0.1:10001', // 开发
        // target: 'http://47.121.28.229:10001', // 生产
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/dev/, '')
      }
    }
  }
})
